import {http} from 'utils/request.js';
import api from '../api/index.js';
import lrz from 'lrz';
import {upLoadOrigin} from '../config';
/** 
 * 获取定位信息
*/
const getLocationInfo = () => {
    const info = {};
    //省份
    info.accidentProvince = sessionStorage.getItem("accidentProvince");
    //市
    info.accidentCity = sessionStorage.getItem("accidentCity");
    //区
    info.accidentArea = sessionStorage.getItem("accidentArea");
    //街道
    info.accidentWhere = sessionStorage.getItem("streetAddr");
    return info;
}

/**
 * 根据key值清除缓存
 * 不传参数清除所有storage
 */
const clearStorage = (key) => {
    if(key) {
        sessionStorage.removeItem(key);
    } else {
        sessionStorage.clear();
    }
}

/** 
 * 添加本地缓存信息
*/
const setStorage = (key,value) => {
    const type = typeof(value);
    if(type=="string") {
        sessionStorage.setItem(key,value);
    } else if(type=="object") {
        sessionStorage.setItem(key,JSON.stringify(value));
    };
}

/**
 * 获取缓存信息
 */
const getStorage = (key="") => {
    if(!key) return;
    return JSON.parse(sessionStorage.getItem(key));
};

/** 
 * 从url上截取参数
*/
const getUrlParams = (paramName) => {
    var reg = new RegExp('(^|&)' + paramName + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return (r[2]);
    };
    return null;
}

/** 
 * 获取事故所有信息
*/
const getAccidentInfo = (accidentNo) => {
    return http.post(api.API_ACCIDENT_DETAIL,{
        accidentNo: accidentNo, //事故编号
    });
}

/** 
 * OCR照片信息
*/
const getOCRResult = (type,id) => {
    return http.post(api.API_OCR_PHOTO,{
        type: type, //车牌识别
        photoId: id
    });
}

//获取图片的token
const getImgToken = () => {
    return http.post(api.API_IMG_TOKEN,{});
}

//压缩图片
const compressImg = (file,qt) => {
    const rate = file.size>200000?0.4:0.7;
   return lrz(file,{
        quality: qt||rate
    });
}

//上传图片
const upLoadImg = (params) => {
    const url = upLoadOrigin + params.timestamp;
    const formData = new FormData();
    formData.append('file', params.file);
    formData.append('token', params.token);
    return http.post(url,formData,http.CTList[1]).then(res=>{
       return res;
    });

    // return fetch(upLoadOrigin + params.timestamp,
    //     {
    //     method:"POST",   //请求方法
    //     body:formData,   //请求体
    //     Accept: 'application/json'
    //   }).then(res=>{
    //        console.log('----网关-----');
    //        console.log(res);
    //       return res.json();
    //   });
}

//根据后台值跳转对应界面
const toJumpPage = (value,params) => {
    if(!value) return;
    const obj = {
        "1" : {
            name: "accidentPic",
            query: {
                accidentNo: params.accidentNo
            }
        },  //上传事故界面
        "2" : {
            name: "evacuateOfScene",
            query: {
                accidentNo: params.accidentNo
            }
        }, //提示挪车界面
        "3" : {
            name: "intelligentResult",
            query: {
                accidentNo: params.accidentNo
            }
        }, //a不同意
        "4" : {
            name: "oppositeSideInfo",
            query: {
                accidentNo: params.accidentNo
            }
        }, //扫码界面
        "5" : {
            name: "supplement",
            query: {
                accidentNo: params.accidentNo,
                mobile: params.mobile,
                fromPage: "handle"
            }
        }, //录入双方信息界面
        "6" : {
            name: "supplement",
            query: {
                accidentNo: params.accidentNo,
                mobile: params.mobile,
            }
        },  //录入个人界面
        "7" : {
            name: "cognizance",
            query: {
                isAllAgree: true,
                accidentNo: params.accidentNo,
            }
        },  //倒计时
        "8" : {
            name: "cognizance",
            query: {
                accidentNo: params.accidentNo
            }
        },   //同上 没有倒计时
        "9" : {
            name: "responsibility",
            query: {
                accidentNo: params.accidentNo 
            }
        }, //pdf查看
        "10" : {
            name: "lastPage",
            query: {
                accidentNo: params.accidentNo
            }
        },  //b方提示a正在录入界面
        "11" : {
            name: "intelligentResult_B",
            query: {
                accidentNo: params.accidentNo,
                // carId:"",
                // fromTo:""
            }
        }, //bf同意不同意 --不会出现这种情况
    }
    return obj[value];
}


export {
    getLocationInfo,
    clearStorage,
    setStorage,
    getStorage,
    getUrlParams,
    getAccidentInfo,
    getOCRResult,
    getImgToken,
    upLoadImg,
    toJumpPage,
    compressImg
}